package com.xuelang.pipeline.artifact.infrastructure.resource.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.artifact.client.common.constant.Constant;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.common.util.MavenUtil;
import com.xuelang.pipeline.artifact.client.resource.dto.RepositoryPackageQuery;
import com.xuelang.pipeline.artifact.domain.resource.RepositoryPackage;
import com.xuelang.pipeline.artifact.domain.resource.repository.RepositoryPackageRepository;
import com.xuelang.pipeline.artifact.infrastructure.resource.converter.RepositoryPackageToRepositoryPackageDOConverter;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactAttributeDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.RepositoryPackageDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.mapper.RepositoryPackageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 仓库制品包仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class RepositoryPackageRepositoryImpl implements RepositoryPackageRepository {

    @Autowired
    RepositoryPackageMapper repositoryPackageMapper;

    @Override
    public PageRecord<RepositoryPackage> pageList(long currentPage, long pageSize, RepositoryPackageQuery repositoryPackageQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<RepositoryPackage> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<RepositoryPackageDO> wrapper = new MPJLambdaWrapper<RepositoryPackageDO>();
        wrapper.selectAll(RepositoryPackageDO.class)
                //.eq(RepositoryPackageDO::getFormat, StrUtil.isNotBlank(repositoryPackageQuery.getFormat()) ? repositoryPackageQuery.getFormat() : Constant.DEFAULT_DB_NOT_EXISTS_DATA)
                .eq(RepositoryPackageDO::getArtifactUuid, StrUtil.isNotBlank(repositoryPackageQuery.getArtifactUuid()) ? repositoryPackageQuery.getArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.and(StrUtil.isNotBlank(repositoryPackageQuery.getCondition()), wp -> wp
                        .like(RepositoryPackageDO::getPackageName, repositoryPackageQuery.getCondition()).or()
                        .like(RepositoryPackageDO::getNameId, repositoryPackageQuery.getCondition()).or()
                        .like(RepositoryPackageDO::getGroupId, repositoryPackageQuery.getCondition()).or()
                        .like(RepositoryPackageDO::getInstanceRepositoryName, repositoryPackageQuery.getCondition()))
                .apply(StrUtil.isNotBlank(repositoryPackageQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", repositoryPackageQuery.getStartDate())
                .apply(StrUtil.isNotBlank(repositoryPackageQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", repositoryPackageQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        repositoryPackageMapper.selectJoinPage(page, RepositoryPackage.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords()
        );
    }

    @Override
    public List<RepositoryPackage> list(RepositoryPackageQuery repositoryPackageQuery) {
        MPJLambdaWrapper<RepositoryPackageDO> wrapper = new MPJLambdaWrapper<RepositoryPackageDO>();
        wrapper.selectAll(RepositoryPackageDO.class)
                //.eq(RepositoryPackageDO::getFormat, StrUtil.isNotBlank(repositoryPackageQuery.getFormat()) ? repositoryPackageQuery.getFormat() : Constant.DEFAULT_DB_NOT_EXISTS_DATA)
                .eq(RepositoryPackageDO::getArtifactUuid, StrUtil.isNotBlank(repositoryPackageQuery.getArtifactUuid()) ? repositoryPackageQuery.getArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);        wrapper.and(StrUtil.isNotBlank(repositoryPackageQuery.getCondition()), wp -> wp
                        .like(RepositoryPackageDO::getPackageName, repositoryPackageQuery.getCondition()).or()
                        .like(RepositoryPackageDO::getNameId, repositoryPackageQuery.getCondition()).or()
                        .like(RepositoryPackageDO::getGroupId, repositoryPackageQuery.getCondition()).or()
                        .like(RepositoryPackageDO::getInstanceRepositoryName, repositoryPackageQuery.getCondition()))
                .apply(StrUtil.isNotBlank(repositoryPackageQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", repositoryPackageQuery.getStartDate())
                .apply(StrUtil.isNotBlank(repositoryPackageQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", repositoryPackageQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        return repositoryPackageMapper.selectJoinList(RepositoryPackage.class, wrapper);
    }


    @Override
    public Boolean save(RepositoryPackage repositoryPackage) {
        return repositoryPackageMapper.insert(RepositoryPackageToRepositoryPackageDOConverter.INSTANCE.repositoryPackageToRepositoryPackageDO(repositoryPackage)) == 1;
    }

    @Override
    public Boolean update(RepositoryPackage repositoryPackage) {
        return repositoryPackageMapper.update(
                RepositoryPackageToRepositoryPackageDOConverter.INSTANCE.repositoryPackageToRepositoryPackageDO(repositoryPackage),
                Wrappers.lambdaQuery(RepositoryPackageDO.class).eq(RepositoryPackageDO::getUuid, repositoryPackage.getUuid())) == 1;
    }

    @Override
    public Boolean delete(String repositoryPackageUuid) {
        return repositoryPackageMapper.delete(Wrappers.lambdaQuery(RepositoryPackageDO.class)
                .eq(RepositoryPackageDO::getUuid, repositoryPackageUuid)) == 1;
    }

    @Override
    public RepositoryPackage query(String repositoryPackageUuid) {
        RepositoryPackageDO repositoryPackageDO = repositoryPackageMapper.selectOne(Wrappers.lambdaQuery(RepositoryPackageDO.class).eq(RepositoryPackageDO::getUuid, repositoryPackageUuid));
        return RepositoryPackageToRepositoryPackageDOConverter.INSTANCE.repositoryPackageDOToRepositoryPackage(repositoryPackageDO);
    }

    @Override
    public RepositoryPackage queryByPackage(String instanceRepositoryName, String name, String group, String format) {
        MPJLambdaWrapper<RepositoryPackageDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(instanceRepositoryName), RepositoryPackageDO::getInstanceRepositoryName, instanceRepositoryName);
        wrapper.eq(StrUtil.isNotBlank(name), RepositoryPackageDO::getNameId, name);
        wrapper.eq(StrUtil.isNotBlank(group), RepositoryPackageDO::getGroupId, group);
        wrapper.eq(StrUtil.isNotBlank(format), RepositoryPackageDO::getFormat, format);
        wrapper.eq(StrUtil.isNotBlank(MavenUtil.joinGroupAndName(group, name)), RepositoryPackageDO::getPackageName, MavenUtil.joinGroupAndName(group, name));
        return repositoryPackageMapper.selectJoinOne(RepositoryPackage.class, wrapper);
    }

    @Override
    public void updateByPackage(String instanceRepositoryName, String name, String group, String format, String version) {
        repositoryPackageMapper.updateVersionAndCount(instanceRepositoryName, name, group, format, version);
    }

    @Override
    public RepositoryPackage queryByPackageName(String instanceRepositoryName, String packageName) {
        MPJLambdaWrapper<RepositoryPackageDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(instanceRepositoryName), RepositoryPackageDO::getInstanceRepositoryName, instanceRepositoryName);
        wrapper.eq(StrUtil.isNotBlank(packageName), RepositoryPackageDO::getPackageName, packageName);
        return repositoryPackageMapper.selectJoinOne(RepositoryPackage.class, wrapper);
    }

    @Override
    public void updateByPackageName(String instanceRepositoryName, String packageName, String version, String format) {
        repositoryPackageMapper.updateVersionAndCountByPackageName(instanceRepositoryName, packageName, version, format);
    }

}
